package bb.lanxing.manager;

import android.content.SharedPreferences;
import android.text.TextUtils;


import bb.lanxing.App;
import bb.lanxing.util.Log;
import bb.lanxing.util.map.MapConfigs;
import java.util.HashMap;

public class BaseSharedPreference implements ISharedPreference {
    private static final String TAG = "BaseSharedPreference";
    private HashMap<String, Object> settings = new HashMap<>();
    private SharedPreferences sharedPreferences;

    private boolean isClassic(int i) {
        return i < 999;
    }

    public BaseSharedPreference(SharedPreferences sharedPreferences) {
        this.sharedPreferences = sharedPreferences;
    }

    public void release() {
        this.sharedPreferences = null;
        HashMap<String, Object> hashMap = this.settings;
        if (hashMap != null) {
            hashMap.clear();
            this.settings = null;
        }
    }

    @Override
    public void registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
        SharedPreferences sharedPreferences = this.sharedPreferences;
        if (sharedPreferences != null) {
            sharedPreferences.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
        }
    }

    @Override
    public void unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
        SharedPreferences sharedPreferences = this.sharedPreferences;
        if (sharedPreferences != null) {
            sharedPreferences.unregisterOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
        }
    }

    @Override
    public void setValue(String str, Object obj) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        SharedPreferences.Editor edit = this.sharedPreferences.edit();
        if (obj instanceof Integer) {
            edit.putInt(str, (Integer) obj);
        } else if (obj instanceof Boolean) {
            edit.putBoolean(str, (Boolean) obj);
        } else if (obj instanceof String) {
            edit.putString(str, (String) obj);
        } else if (obj instanceof Long) {
            edit.putLong(str, (Long) obj);
        } else if (obj instanceof Float) {
            edit.putFloat(str, (Float) obj);
        } else if (obj instanceof Enum) {
            edit.putString(str, ((Enum) obj).name());
        }
        edit.apply();
        this.settings.put(str, obj);
    }

    @Override
    public int getInt(String str, int i) {
        if (TextUtils.isEmpty(str)) {
            return i;
        }
        Object obj = this.settings.get(str);
        if (obj == null) {
            obj = this.sharedPreferences.getInt(str, i);
            this.settings.put(str, obj);
        }
        try {
            if (str.equals(MapConfigs.MULTI_MAP_TILE) && !isClassic((Integer) obj)) {
                return 0;
            }
            if (!str.equals(MapConfigs.MULTI_MAP_TYPE)) {
                return (Integer) obj;
            }
            return 1;
        } catch (Exception e) {
            if (App.getContext().isDebugMode()) {
                Log.w(TAG, "getInt: get key [" + str + "] class is" + obj.getClass());
                e.printStackTrace();
            }
            return i;
        }
    }

    @Override
    public boolean getBoolean(String str, boolean z) {
        if (TextUtils.isEmpty(str)) {
            return z;
        }
        Object obj = this.settings.get(str);
        if (obj == null) {
            obj = this.sharedPreferences.getBoolean(str, z);
            this.settings.put(str, obj);
        }
        try {
            return (Boolean) obj;
        } catch (Exception e) {
            if (App.getContext().isDebugMode()) {
                Log.w(TAG, "getBoolean: get key [" + str + "] class is" + obj.getClass());
                e.printStackTrace();
            }
            return z;
        }
    }

    @Override
    public String getString(String str, String str2) {
        if (TextUtils.isEmpty(str)) {
            return str2;
        }
        Object obj = this.settings.get(str);
        if (obj == null) {
            obj = this.sharedPreferences.getString(str, str2);

            this.settings.put(str, obj);
        }
        try {
            String str3 = (String) obj;
            if (str3 == null){
                return str2;
            }
            return str3.isEmpty() ? str2 : str3;
        } catch (Exception e) {
            if (App.getContext().isDebugMode()) {
                if (obj != null) {
                    Log.w(TAG, "getString: get key [" + str + "] class is" + obj.getClass());
                }
                e.printStackTrace();
            }
            return str2;
        }
    }

    @Override
    public long getLong(String str, long j) {
        if (TextUtils.isEmpty(str)) {
            return j;
        }
        Object obj = this.settings.get(str);
        if (obj == null) {
            obj = this.sharedPreferences.getLong(str, j);
            this.settings.put(str, obj);
        }
        try {
            return (Long) obj;
        } catch (Exception e) {
            if (App.getContext().isDebugMode()) {
                Log.w(TAG, "getLong: get key [" + str + "] class is" + obj.getClass());
                e.printStackTrace();
            }
            return j;
        }
    }

    @Override
    public float getFloat(String str, float f) {
        if (TextUtils.isEmpty(str)) {
            return f;
        }
        Object obj = this.settings.get(str);
        if (obj == null) {
            obj = this.sharedPreferences.getFloat(str, f);
            this.settings.put(str, obj);
        }
        try {
            return (Float) obj;
        } catch (Exception e) {
            if (App.getContext().isDebugMode()) {
                Log.w(TAG, "getFloat: get key [" + str + "] class is" + obj.getClass());
                e.printStackTrace();
            }
            return f;
        }
    }

    @Override
    public Enum getEnum(String str, Enum r6) {
        if (TextUtils.isEmpty(str)) {
            return r6;
        }
        Object obj = this.settings.get(str);
        if (obj == null) {
            obj = Enum.valueOf(r6.getClass(), this.sharedPreferences.getString(str, r6.name()));
            this.settings.put(str, obj);
        }
        try {
            return (Enum) obj;
        } catch (Exception e) {
            if (App.getContext().isDebugMode()) {
                Log.w(TAG, "getFloat: get key [" + str + "] class is" + obj.getClass());
                e.printStackTrace();
            }
            return r6;
        }
    }

    @Override
    public boolean contains(String str) {
        return this.sharedPreferences.contains(str);
    }

    @Override
    public void remove(String str) {
        this.settings.remove(str);
        this.sharedPreferences.edit().remove(str).apply();
    }

    @Override
    public void commit() {
        this.sharedPreferences.edit().apply();
    }
}
